<?php
namespace Staff\Controller;
use Think\Controller;
use Think\Image;
class FoodController extends AuthController {
	/**
	 * 修改食物类型
	 */
	public function UpdateFoodCategory(){
		$id = I("id");
		$where['id'] = $id;
		$result = M("FoodCategory")->where($where)->save($_POST);
		$this->ajaxReturn($result);
	}
	/**
	 * 添加食物类型
	 */
	public function addFoodCategory(){
		$result = M("FoodCategory")->add($_POST);
		$this->ajaxReturn($result);
	}
	/**
	 * 删除食物类型
	 */
	public function delFoodCategory(){
		$id = I("id");
		$where['id'] = $id;
		$result = M("FoodCategory")->where($where)->delete();
		$this->ajaxReturn($result);
	}
	/**
	 * 获取食物类型
	 */
	public function getFoodCategory(){
		$fid = I("fid");
		$where['fid'] = $fid;
		$list = M("FoodCategory")->where($where)->select();
		$this->ajaxReturn($list);
	}
	public function index(){
		$Food=D('FoodView');
		$list = $Food->group('id')->order(array('id'=>'desc'))->select();
// 		$type = S("FoodType");
// 		if(!$type){
// 			$type = D("FoodType")->cache("FoodType")->order(array('id'=>'asc'))->select();
// 		}
		$type = D("FoodType")->order(array('id'=>'asc'))->select();
// 		$unit = S("unit");
// 		if(!$unit){
// 			$unit = D("unit")->cache("unit")->order(array('id'=>'asc'))->select();
// 		}
		$unit = D("unit")->order(array('id'=>'asc'))->select();
		$this->assign('list',$list);
// 		var_dump($list);
		$this->assign('type',$type);
		$this->assign('unit',$unit);
		$this->display();
	}
	public function getFoodTag(){
		$type = I('type');
		if($type!=null&&$type!=""){
			$tag = M('FoodTag')->where(array('food_type'=>$type))->select();
			$this->ajaxReturn($tag);
		}
	}
	public function getInfo(){
		$id = I('id');
		if(isset($id)){
			$info=D("Food")->relation(true)->where(array('id'=>$id))->find();
			$this->ajaxReturn($info,'json');
		}
	}
	public function editFood(){
		if(IS_POST){
			$file = $_FILES['fileField'];
			if($file['size']>0){
				$uploadpath = "Public/Upload/Food/";//上传路径
				if (!file_exists($uploadpath)){
					mkdir($uploadpath, 0777);
				}
				$pinfo = pathinfo($file['name']);
				$ftype = $pinfo['extension'];//文件类型
				$name = basename($file['name'],".{$ftype}");
					
				$fileName = md5($name.time()).".".$ftype;//识别图
				$image = new Image();
				$image->open($file['tmp_name']);
				$image->save($uploadpath.$fileName);
				$imgurl = 'http://'.$_SERVER['HTTP_HOST'].'/'.__ROOT__.'/'.$uploadpath.$fileName;
			}else{
				$imgurl=M('Food')->where(array('id'=>I('id')))->getField('food_image');
			}
			$data = array(
					'food_name'	=> I('name'),
					'type'  => I('type'),
// 					'price'  => I('price'),
// 					'yprice'  => I('yprice'),
// 					'unit'    => I('unit'),
					'introduce'	=> I('introduce'),
					'food_tag'=>I('tag'),
					'food_image'=>$imgurl
			);
			$Food = D('Food');
			$fid = I('id');
			if($Food->create($data)){
				$result=$Food->where(array('id'=>$fid))->save($data);
				M('FoodCategory')->where(array('fid'=>$fid))->delete();
				$category = I("category");
				$price    = I("price");
				$yprice   = I("yprice");
				$unit     = I("unit");
				$count = count($category);
				for($i = 0;$i<$count;$i++){
					$category_item = array(
							'fid'      =>$fid,
							'title'    =>$category[$i],
							'price'    =>$price[$i],
							'yprice'   =>$yprice[$i],
							'unit'     =>$unit[$i],
					);
					$category_data[] = $category_item;
				}
				$result2 = M("FoodCategory")->addAll($category_data);
				if($result || $result2){
					$this->success("修改成功","index");
				}else{
					$this->error("修改失败");
				}
			}else{
				$this->error($Food->getError());
			}
		}
	}
	
	public function addFood(){
		if(IS_POST){
			$file = $_FILES['fileField'];
			if($file['size']>0){
				$uploadpath = "Public/Upload/Food/";//上传路径
				if (!file_exists($uploadpath)){
					mkdir($uploadpath, 0777);
				}
				$pinfo = pathinfo($file['name']);
				$ftype = $pinfo['extension'];//文件类型
				$name = basename($file['name'],".{$ftype}");
					
				$fileName = md5($name.time()).".".$ftype;//识别图
				$image = new Image();
				$image->open($file['tmp_name']);
				$image->save($uploadpath.$fileName);
				$imgurl = 'http://'.$_SERVER['HTTP_HOST'].'/'.__ROOT__.'/'.$uploadpath.$fileName;
			}
			$data = array(
					'food_name'	=> I('name'),
					'type'  => I('type'),
// 					'price'  => I('price'),
// 					'yprice'  => I('yprice'),
// 					'unit'    => I('unit'),
					'introduce'	=> I('introduce'),
					'food_tag'=>I('tag'),
					'food_image'=>$imgurl
			);
			$Food = D('Food');
			$data = $Food->create($data);
			if($data){
// 				$data["FoodCategory"] = $category_data;
				$result=$Food->add($data);
				if($result){
					$category = I("category");
					$price    = I("price");
					$yprice   = I("yprice");
					$unit     = I("unit");
					$count = count($category);
					for($i = 0;$i<$count;$i++){
						$category_data[] = array(
								'fid'      =>$result,
								'title'    =>$category[$i],
								'price'    =>$price[$i],
								'yprice'   =>$yprice[$i],
								'unit'     =>$unit[$i],
						);
					}
					M("FoodCategory")->addAll($category_data);
					$this->success("添加成功","index");
				}else{
					$this->error("添加失败");
				}
			}else{
				$this->error($Food->getError());
			}		
		}
	}
}